<?php

class Model_ShowAPI {

    /**
     * @var string
     * @url https://www.showapi.com/
     * 应用秘钥 请前往官网注册获得
     */
    private $ApiKey = '96a1511eeba84e24a2c860366c5c6d11';
    /**
     * @var string
     * @url https://www.showapi.com/
     * 应用ID 请前往官网注册获得
     */
    private $ApiId = '10809';

    public function getWeatherInfo( $areaId, $area, $needMoreDay, $needIndex, $needHourData ) {
        $paramArr = [
            'showapi_appid'=> $this->ApiId,
            'areaid' => $areaId,
            'area' => $area,
            'needMoreDay' => $needMoreDay,
            'needIndex' => $needIndex,
            'needHourData' => $needHourData,
            'showapi_timestamp' => date( 'YmdHis' )
        ];
        $strParam = $this->createStrParam($paramArr);
        $url = 'http://route.showapi.com/9-2?'.$strParam;
        $result = file_get_contents($url);
        $result = json_decode($result,true)['showapi_res_body'];
        return $result;
    }

    private function createSign ( $paramArr ) {
        $sign = '';
        ksort($paramArr);
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $sign .= $key.$val;
            }
        }
        $sign .= $this->ApiKey;
        $sign = strtoupper( md5( $sign ) );
        return $sign;
    }

    private function createStrParam ($paramArr) {
        $strParam = '';
        $sign = $this->createSign($paramArr);
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $strParam .= $key.'='.urlencode($val).'&';
            }
        }
        $strParam .= 'showapi_sign='.$sign;
        return $strParam;
    }

}